#!/bin/bash

status_file="`dirname $0`/.`basename $0`.dat"
router_ip="192.168.1.1"

function get_info(){
  info_name="$1"
# <TD ALIGN=RIGHT><B>IP Address</B></TD>
# <TD>16.190.193.1 &nbsp;</TD>
  cat $status_file | awk 'BEGIN{found=0; area=0;}{\
    if ($0 ~ "<TD COLSPAN=2><B>LAN</B></TD></TR>") {area=1;}\
    if ($0 ~ "<TD COLSPAN=2><B>WAN</B></TD></TR>") {area=2;}\
    if ($0 ~ "<TD COLSPAN=2><B>Backup WAN</B></TD></TR>") {area=3;}\
    if (found == 1 && area == 2) {\
      gsub("&nbsp;", "", $0);\
      gsub("<TD>", "", $0);\
      gsub("</TD>", "", $0);\
      printf("'"$info_name"':%s\n",$0);\
    }\
    if (found == 1) {found = 0;}\
    if ($0 ~ ">'"$info_name"'<") {found = 1;}\
  }'
}

function WAN_status(){
  http_proxy=""
  wget --output-document="$status_file" \
       --head "Authorization: Basic YWRtaW46emRoczE2MTY=" \
       "http://$router_ip/status.htm" >/dev/null 2>&1
  get_info "MAC Address"
  get_info "IP Address"
  get_info "Subnet Mask"
  get_info "Gateway"
  get_info "Domain Name Server"
  get_info "Remaining Lease Time"
}

WAN_status
exit 0

function wifistatus(){
  http_proxy=""
  wget --output-document="$status_file" --head "Authorization: Basic YWRtaW46emRoczE2" \
    "http://192.168.1.2/wlap.htm?rc=&rf=0100&ZT=1346049340805" 1>/dev/null 2>&1

  cat $status_file | grep "wdv0=(\"0000\"==\"0000\")?true:false" >/dev/null
  [ $? -eq 0 ] && echo "Actived"

  cat $status_file | grep "wdv0=(\"0002\"==\"0000\")?true:false" >/dev/null
  [ $? -eq 0 ] && echo "Stopped"

}

function shutdown(){
  http_proxy=""
  wget --output-document="$status_file" --head "Authorization: Basic YWRtaW46emRoczE2" \
    "http://192.168.1.2/cgi-bin/wlap?RC=%40wlap&rf=0100&rd=x&Xf=1&prev=0100&CS=4&WD=x&ZN=CMCC-AP1&ZC=9&_Security=5&KM%23=00FF&KM4=o&KM5=x&PK=zdhs1616" 1>/dev/null 2>&1
  sleep 2
  wget --output-document="$status_file" --head "Authorization: Basic YWRtaW46emRoczE2" \
    "http://192.168.1.2/cgi-bin/rebo?D=&ZT=1346051366928" 1>/dev/null 2>&1
}

function startup(){
  http_proxy=""
  wget --output-document="$status_file" --head "Authorization: Basic YWRtaW46emRoczE2" \
    "http://192.168.1.2/cgi-bin/wlap?RC=%40wlap&rf=0100&rd=x&Xf=1&prev=0100&CS=4&WD=o&ZN=CMCC-AP1&ZC=9&_Security=5&KM%23=00FF&KM4=o&KM5=x&PK=zdhs1616" 1>/dev/null 2>&1
  sleep 2
  wget --output-document="$status_file" --head "Authorization: Basic YWRtaW46emRoczE2" \
    "http://192.168.1.2/cgi-bin/rebo?D=&ZT=1346051503387" 1>/dev/null 2>&1
}

echo "DI624 WIFI stauts: "`wifistatus`

echo "Shuting down WIFI ......"
shutdown 1>/dev/null 2>&1
sleep 10
echo "DI624 WIFI stauts: "`wifistatus`

echo "Starting up WIFI ......"
startup 1>/dev/null 2>&1
sleep 10
echo "DI624 WIFI stauts: "`wifistatus`

# Using Wget To Download Content Protected By Referer And Cookies 
# wget --cookies=on --keep-session-cookies --save-cookies=cookie.txt http://first_page
# wget --referer=http://first_page --cookies=on --load-cookies=cookie.txt --keep-session-cookies --save-cookies=cookie.txt http://second_page


